/**
 * Desc :
 * Created by byte2 on 2016-12-26.
 */
import Lang from '../../libs/i18';
import FileUpload from "react-fileupload";
import {URL} from "../../libs/Url";
export default React.createClass({
    propTypes: {
        onsuccess: React.PropTypes.func
    },
    getDefaultProps(){
        return {
            multiple: false,//默认单文件
        }
    },
    getInitialState(){
        return {
            preview: ''
        }
    },
    componentWillReceiveProps(nextProps){

        if (nextProps.defaultValue) {
            this.setState({
                preview: nextProps.defaultValue ? URL.FUN_UPLOAD_PREVIEW(nextProps.defaultValue) : ''
            });
        }
    },
    render(){
        const self = this;
        var options = {
            baseUrl: self.props.url || URL.Get_UPLOAD,
            fileFieldName: 'file',
            param: {
                fid: 0
            },
            multiple: self.props.multiple,//多文件或单文件
            chooseAndUpload: true,
            accept: 'image/*',
            withCredentials: true,
            // beforeChoose: function () {
            //     return user.isAllowUpload
            // },
            chooseFile: function (files) {
                console.log('you choose', typeof files == 'string' ? files : files[0].name)
            },
            beforeUpload: function (files, mill) {
                if (typeof files == 'string') return true;
                if (files[0].size < 1024 * 1024 * 20) {
                    files[0].mill = mill;
                    return true
                }
                return false
            },
            doUpload: function (files, mill) {
                console.log('you just uploaded', typeof files == 'string' ? files : files[0].name)
            },
            uploading: function (progress) {
                console.log('loading...', progress.loaded / progress.total + '%')
            },
            uploadSuccess: function (resp) {
                console.log('upload success..!' + resp.result.data.image);
                self.setState({
                    preview: URL.FUN_UPLOAD_PREVIEW(resp.result.data.image.photoid)
                });
                self.props.onsuccess(resp.result.data.image.photoid)
            },
            uploadError: function (err) {
                // console.log(err.message);
                alert(err.message)
            },
            uploadFail: function (resp) {
                // console.log(resp);
                alert(resp)
            }
        };
        const preview = this.state.preview ? this.state.preview : (
            this.props.defaultValue ? URL.FUN_UPLOAD_PREVIEW(this.props.defaultValue) : ''
        );
        return (
            <div>

                <FileUpload options={options}>
                    <img ref="chooseAndUpload" style={{width: '50%'}} src={preview || require('../../imgs/no_image.png')} alt=""/>
                    {/*<button className="btn btn-default btn-sm" ref="chooseAndUpload">*/}
                        {/*<i className="fa fa-upload"></i>*/}
                        {/*{Lang.UPLOAD.CHOOSE}*/}
                    {/*</button>*/}
                </FileUpload>
            </div>
        )
    }
})
